#include "GSWeekday.h"
#include "GSSaturday.h"

GSWeekday::GSWeekday()
{
	SetStateName("GSWEEKDAY");
}

GSWeekday::~GSWeekday()
{
	Game::GetDevice()->GetDriver()->removeTexture(sprWDTexture);
	Game::GetDevice()->GetDriver()->removeTexture(sprHolder);
	Game::GetDevice()->GetDriver()->removeTexture(sprMorning);
	Game::GetDevice()->GetDriver()->removeTexture(sprAfterSchool);
	Game::GetDevice()->GetDriver()->removeTexture(sprEvening);
	SAFE_DEL(sprWDTexture);
	SAFE_DEL(sprMorning);
	SAFE_DEL(sprAfterSchool);
	SAFE_DEL(sprEvening);
	SAFE_DEL(sprHolder);
}

void GSWeekday::OnEnter()
{
	fontCustom = Game::GetDevice()->GetInstance()->getGUIEnvironment()->getFont("data/strings/fontcourier.bmp");
	fontCustomBig = Game::GetDevice()->GetInstance()->getGUIEnvironment()->getFont("data/strings/bigfont.png");
	sprWDTexture = Game::GetDevice()->GetDriver()->getTexture("data/sprites/spr_weekday.png");
	sprMorning = NULL;
	sprAfterSchool = NULL;
	sprEvening = NULL;
	dayBox = core::rect<s32>(10, 11, 120, 121);
	bgBox = core::rect<s32>(0, 0, 1024, 600);
	m_IsNextDay = false;
}

void GSWeekday::OnExit()
{
	srand(time(NULL));
}

void GSWeekday::Update()
{
	GetDayName();
	switch(Game::GetInstance()->GetDayTime())
	{
	case DAYTIME_MORNING:
		{
			if(sprMorning == NULL)
				sprMorning = Game::GetDevice()->GetDriver()->getTexture("data/sprites/bg/Draft_Morning.png");
			if(sprEvening != NULL)
			{
				Game::GetDevice()->GetDriver()->removeTexture(sprEvening);
				sprEvening = NULL;
			}
			if(Game::GetDevice()->receiver.IsKeyUp(KEY_KEY_1))
			{
				Game::GetInstance()->GetPlayer()->SubCurrentDayPoints(POINT_SUBTRACT_VALUE_MAIN);
				Game::GetInstance()->GetPlayer()->SetAttribute(ATTRIB_MATH, Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_MATH) + ADD_PER_DAY);
				DEBUG_LOG("Math: %d | Science: %d | PE: %d\n", Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_MATH), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_SCIENCE), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_PE));
				Game::GetInstance()->NextDayTime();
			}
			else if(Game::GetDevice()->receiver.IsKeyUp(KEY_KEY_2))
			{
				Game::GetInstance()->GetPlayer()->SubCurrentDayPoints(POINT_SUBTRACT_VALUE_MAIN);
				Game::GetInstance()->GetPlayer()->SetAttribute(ATTRIB_SCIENCE, Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_SCIENCE) + ADD_PER_DAY);
				DEBUG_LOG("Math: %d | Science: %d | PE: %d\n", Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_MATH), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_SCIENCE), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_PE));
				Game::GetInstance()->NextDayTime();
			}
			else if(Game::GetDevice()->receiver.IsKeyUp(KEY_KEY_3))
			{
				Game::GetInstance()->GetPlayer()->SubCurrentDayPoints(POINT_SUBTRACT_VALUE_MAIN);
				Game::GetInstance()->GetPlayer()->SetAttribute(ATTRIB_PE, Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_PE) + ADD_PER_DAY);
				DEBUG_LOG("Math: %d | Science: %d | PE: %d\n", Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_MATH), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_SCIENCE), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_PE));
				Game::GetInstance()->NextDayTime();
			}
			sprHolder = sprMorning;
			break;
		}
	case DAYTIME_AFTERSCHOOL:
		{
			if(sprAfterSchool == NULL)
				sprAfterSchool = Game::GetDevice()->GetDriver()->getTexture("data/sprites/bg/Draft_AfterSchool.png");
			if(sprMorning != NULL)
			{
				Game::GetDevice()->GetDriver()->removeTexture(sprMorning);
				sprMorning = NULL;
			}
			if(Game::GetDevice()->receiver.IsKeyUp(KEY_KEY_1))
			{
				int rAttrib = RandomAttribute();
				Game::GetInstance()->GetPlayer()->SubCurrentDayPoints(POINT_SUBTRACT_VALUE_MAIN);
				Game::GetInstance()->GetPlayer()->SetAttribute(rAttrib, Game::GetInstance()->GetPlayer()->GetAttribute(rAttrib) + ADD_PER_DAY);
				DEBUG_LOG("Math: %d | Science: %d | PE: %d\n", Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_MATH), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_SCIENCE), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_PE));
				Game::GetInstance()->NextDayTime();
			}
			if(Game::GetDevice()->receiver.IsKeyUp(KEY_KEY_2))
			{
				Game::GetInstance()->GetPlayer()->SubCurrentDayPoints(POINT_SUBTRACT_VALUE_MAIN);
				Game::GetInstance()->GetPlayer()->SetAttribute(Game::GetInstance()->GetPlayer()->GetPlayerMainAttribute(), Game::GetInstance()->GetPlayer()->GetAttribute(Game::GetInstance()->GetPlayer()->GetPlayerMainAttribute()) + ADD_PER_DAY);
				DEBUG_LOG("Math: %d | Science: %d | PE: %d\n", Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_MATH), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_SCIENCE), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_PE));
				Game::GetInstance()->NextDayTime();
			}
			if(Game::GetDevice()->receiver.IsKeyUp(KEY_KEY_3))
			{
				Game::GetInstance()->NextDayTime();
			}
			sprHolder = sprAfterSchool;
			break;
		}
	case DAYTIME_EVENING:
		{
			if(sprEvening == NULL)
				sprEvening = Game::GetDevice()->GetDriver()->getTexture("data/sprites/bg/Draft_Evening.png");
			if(sprAfterSchool != NULL)
			{
				Game::GetDevice()->GetDriver()->removeTexture(sprAfterSchool);
				sprAfterSchool = NULL;
			}
			if(Game::GetDevice()->receiver.IsKeyUp(KEY_KEY_1))
			{
				Game::GetInstance()->GetPlayer()->SubCurrentDayPoints(POINT_SUBTRACT_VALUE_MAIN);
				int rAttrib = RandomAttribute();
				Game::GetInstance()->GetPlayer()->SetAttribute(rAttrib, Game::GetInstance()->GetPlayer()->GetAttribute(rAttrib) + ADD_PER_DAY);
				DEBUG_LOG("Math: %d | Science: %d | PE: %d\n", Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_MATH), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_SCIENCE), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_PE));
				Game::GetInstance()->NextDayTime();
				m_IsNextDay = true;
			}
			if(Game::GetDevice()->receiver.IsKeyUp(KEY_KEY_2))
			{
				Game::GetInstance()->GetPlayer()->SubCurrentDayPoints(POINT_SUBTRACT_VALUE_MAIN);
				Game::GetInstance()->GetPlayer()->SetAttribute(Game::GetInstance()->GetPlayer()->GetPlayerMainAttribute(), Game::GetInstance()->GetPlayer()->GetAttribute(Game::GetInstance()->GetPlayer()->GetPlayerMainAttribute()) + ADD_PER_DAY);
				DEBUG_LOG("Math: %d | Science: %d | PE: %d\n", Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_MATH), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_SCIENCE), Game::GetInstance()->GetPlayer()->GetAttribute(ATTRIB_PE));
				Game::GetInstance()->NextDayTime();
				m_IsNextDay = true;
			}
			if(Game::GetDevice()->receiver.IsKeyUp(KEY_KEY_3))
			{
				Game::GetInstance()->NextDayTime();
				m_IsNextDay = true;
			}
			sprHolder = sprEvening;
			break;
		}
	}
	if(m_IsNextDay)
	{
		m_IsNextDay = false;
		Game::GetInstance()->NextDay();
	}
	if(Game::GetInstance()->GetCurrentDay() > DAYS_FRIDAY)
	{
		Game::PushState((GameState*)new GSSaturday());
	}
}

void GSWeekday::Render()
{
	Game::GetDevice()->GetDriver()->beginScene(true, true, video::SColor(255,255,255,255));

	Game::GetDevice()->GetDriver()->draw2DImage(sprHolder, core::position2di(0, 0), bgBox, 0, video::SColor(255,255,255,255), true);
	Game::GetDevice()->GetDriver()->draw2DImage(sprWDTexture, core::position2di(SCREEN_WIDTH - dayBox.getWidth() - 30, 30), dayBox, 0, video::SColor(255,255,255,255), true);
	fontCustomBig->draw(s_DayName, core::rect<s32>(SCREEN_WIDTH - dayBox.getWidth() - 27, 40, 30, 30), video::SColor(255,255,255,255));
	switch(Game::GetInstance()->GetDayTime())
	{
	case DAYTIME_MORNING:
		{
			fontCustom->draw("1. Study Math", core::rect<s32>(80, 300,100,10), video::SColor(255,0,0,0));
			fontCustom->draw("2. Study Science", core::rect<s32>(80, 312,100,10), video::SColor(255,0,0,0));
			fontCustom->draw("3. Study PE", core::rect<s32>(80, 324,100,10), video::SColor(255,0,0,0));
			break;
		}
	case DAYTIME_AFTERSCHOOL:
		{
			fontCustom->draw("1. Library", core::rect<s32>(80, 300,100,10), video::SColor(255,0,0,0));
			fontCustom->draw("2. Club Activity", core::rect<s32>(80, 312,100,10), video::SColor(255,0,0,0));
			fontCustom->draw("3. Go Home", core::rect<s32>(80, 324,100,10), video::SColor(255,0,0,0));
			break;
		}
	case DAYTIME_EVENING:
		{
			fontCustom->draw("1. Watch TV", core::rect<s32>(80, 300,100,10), video::SColor(255,0,0,0));
			fontCustom->draw("2. Play Game", core::rect<s32>(80, 312,100,10), video::SColor(255,0,0,0));
			fontCustom->draw("3. Go Sleep", core::rect<s32>(80, 324,100,10), video::SColor(255,0,0,0));
			break;
		}
	}
	Game::GetDevice()->GetDriver()->endScene();
}

void GSWeekday::GetDayName()
{
	s_DayName = "";
	switch(Game::GetInstance()->GetCurrentDay())
	{
	case DAYS_MONDAY:
		{
			s_DayName += "MONDAY";
			break;
		}
	case DAYS_TUESDAY:
		{
			s_DayName += "TUESDAY";
			break;
		}
	case DAYS_WEDNESDAY:
		{
			s_DayName += "WEDNESDAY";
			break;
		}
	case DAYS_THURSDAY:
		{
			s_DayName += "THURSDAY";
			break;
		}
	case DAYS_FRIDAY:
		{
			s_DayName += "FRIDAY";
			break;
		}
	default:
		{
			s_DayName += "It's not weekday!";
			break;
		}
	}
}

int GSWeekday::RandomAttribute()
{
	int returnValue = (int)(rand() % 3);
	while(returnValue == Game::GetInstance()->GetPlayer()->GetPlayerMainAttribute())
		returnValue = (int)(rand() % 3);
	return returnValue;
}